#include "ir_command_handler.h"
#include "led_driver.h"
#include "effects.h"
#include "ttl_serial.h"
#include "bluetooth_serial.h"
#include "ir_remote.h"

// 红外遥控命令处理实现
void handleIRCommand(uint32_t command, LEDDriver& ledDriver, BaseEffect*& currentEffect, 
                    int& currentEffectIndex, bool& systemRunning, TTLSerial& ttlSerial, 
                    BluetoothSerial& btSerial) {
    // 声明效果实例（需要在主程序中定义）
    extern StaticColorEffect staticEffect;
    extern BreathingEffect breathingEffect;
    
    switch (command) {
        case IR_CMD_POWER:
            // 切换系统运行状态
            systemRunning = !systemRunning;
            if (systemRunning) {
                ttlSerial.println("System started via IR");
                btSerial.println("System started via IR");
            } else {
                // 清除LED
                ledDriver.clear();
                ttlSerial.println("System stopped via IR");
                btSerial.println("System stopped via IR");
            }
            break;
            
        case IR_CMD_NEXT:
            // 切换到下一个效果
            currentEffectIndex = (currentEffectIndex + 1) % 2;
            switch (currentEffectIndex) {
                case 0:
                    currentEffect = &staticEffect;
                    break;
                case 1:
                    currentEffect = &breathingEffect;
                    break;
            }
            currentEffect->init(&ledDriver);
            ttlSerial.print("Switched to effect via IR: ");
            ttlSerial.println(currentEffect->getName());
            btSerial.print("Switched to effect via IR: ");
            btSerial.println(currentEffect->getName());
            break;
            
        case IR_CMD_MODE:
            // 切换到上一个效果
            currentEffectIndex = (currentEffectIndex + 2 - 1) % 2;
            switch (currentEffectIndex) {
                case 0:
                    currentEffect = &staticEffect;
                    break;
                case 1:
                    currentEffect = &breathingEffect;
                    break;
            }
            currentEffect->init(&ledDriver);
            ttlSerial.print("Switched to effect via IR: ");
            ttlSerial.println(currentEffect->getName());
            btSerial.print("Switched to effect via IR: ");
            btSerial.println(currentEffect->getName());
            break;
            
        case IR_CMD_UP:
            // 增加亮度（仅在系统运行时）
            if (systemRunning) {
                // 这里可以添加亮度控制逻辑
                ttlSerial.println("Brightness increased via IR");
                btSerial.println("Brightness increased via IR");
            }
            break;
            
        case IR_CMD_DOWN:
            // 降低亮度（仅在系统运行时）
            if (systemRunning) {
                // 这里可以添加亮度控制逻辑
                ttlSerial.println("Brightness decreased via IR");
                btSerial.println("Brightness decreased via IR");
            }
            break;
            
        case IR_CMD_PLAY:
            // 暂停/恢复效果更新
            // 这里可以添加暂停/恢复逻辑
            ttlSerial.println("Play/Pause via IR");
            btSerial.println("Play/Pause via IR");
            break;
            
        default:
            // 未知命令
            ttlSerial.print("Unknown IR command: 0x");
            // 转换为十六进制字符串
            char hexStr[9];
            sprintf(hexStr, "%08X", command);
            ttlSerial.println(hexStr);
            btSerial.print("Unknown IR command: 0x");
            btSerial.println(hexStr);
            break;
    }
}